package guoanmaker.operator.business.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.modelnobind.TrademarkSearchLogs;

/**
 * 
* @ClassName: TrademarkSearchLogsDao 
* @Description: TODO(接口历史查询记录) 
* @author (ZhangXinYu)  
* @date 2017年12月15日 下午5:58:19 
* @version V1.0
 */
@Repository
public class TrademarkSearchLogsDao {
	@PersistenceContext
	EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	public List<TrademarkSearchLogs> findTrademarkSearchLogs(String name,String username ,String keyword , String startDate , String endDate, Integer currentPage , Integer pageSize){
		String jpql = "select new  guoanmaker.operator.business.modelnobind.TrademarkSearchLogs(u.nickname,u.username,t.keyword,t.datetime,t.tmName,t.tmCls) from OperatorTrademarkSearchLog t LEFT JOIN t.user u where 1=1";
		if(name != null && !name.equals("")){
			jpql += " and u.nickname like '%"+name+"%'";
		}
		if(username != null && !username.equals("")){
			jpql += " and u.username like '%"+username+"%'";
		}
		if(keyword != null && !keyword.equals("")){
			jpql += " and t.tmName like '%"+keyword+"%'";
		}
		if(startDate != null && !startDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(startDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				//format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
				startDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql += " and t.datetime > '"+startDate+"'";
			System.out.println(startDate);
		}
		if(endDate != null && !endDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(endDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				//设置结束如期为当日23:59:59
				//format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				endDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql += " and t.datetime < '"+endDate+"'";
			System.out.println(endDate);
		}
		jpql += " Order By t.datetime desc";
		Query query = entityManager.createQuery(jpql).setFirstResult(currentPage*pageSize).setMaxResults(pageSize);
		List<TrademarkSearchLogs> trademarkSearchLogs = query.getResultList();
		return trademarkSearchLogs;
	}
	
	@SuppressWarnings("unchecked")
	public Integer findTrademarkSearchLogsCount(String name,String username ,String keyword , String startDate , String endDate){
		String jpql = "select t from OperatorTrademarkSearchLog t LEFT JOIN t.user u where 1=1";
		if(name != null && !name.equals("")){
			jpql += " and u.nickname like '%"+name+"%'";
		}
		if(username != null && !username.equals("")){
			jpql += " and u.username like '%"+username+"%'";
		}
		if(keyword != null && !keyword.equals("")){
			jpql += " and t.tmName like '%"+keyword+"%'";
		}
		/*if((startDate != null && !startDate.equals("")) && (endDate != null && !endDate.equals(""))){
			jpql += " and t.datetime between '"+startDate+"' and '"+endDate+"'";
		}*/
		if(startDate != null && !startDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(startDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				//format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
				startDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql += " and t.datetime > '"+startDate+"'";
		}
		if(endDate != null && !endDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(endDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				//设置结束如期为当日23:59:59
				//format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				endDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql += " and t.datetime < '"+endDate+"'";
		}
		Query query = entityManager.createQuery(jpql);
		List<TrademarkSearchLogs> trademarkSearchLogs = query.getResultList();
		return trademarkSearchLogs.size();
	}
	
}
